package com.wmcsk.dl.dl;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager.LayoutParams;

import com.wmcsk.dl.dl.internal.DLAttachable;
import com.wmcsk.dl.dl.internal.DLPluginManager;
import com.wmcsk.dl.dl.internal.DLProxyImpl;


/**
 * 透明activity代理
 */
public class DLTranslucentProxyActivity extends Activity implements DLAttachable {

    private static final String TAG = DLTranslucentProxyActivity.class.getName();
    protected DLPlugin mRemoteActivity;
    private DLProxyImpl impl = new DLProxyImpl(this);
    public boolean visibleActivity = true; //默认插件可见可跳转

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        DL.e("动画：" + getResources().getIdentifier("Anim.Translucent.activity", "style", getPackageName()));
//        getWindow().setWindowAnimations(getResources().getIdentifier("Anim.Translucent.activity", "style", getPackageName()));
        setVisibleActivity(true); //新界面默认可见
        impl.onCreate(getIntent());

    }

    @Override
    public void attach(DLPlugin remoteActivity, DLPluginManager pluginManager) {
        mRemoteActivity = remoteActivity;
    }

    @Override
    public AssetManager getAssets() {
        return impl.getAssets() == null ? super.getAssets() : impl.getAssets();

    }

    @Override
    public Resources getResources() {
        return impl.getResources() == null ? super.getResources() : impl.getResources();

    }

    @Override
    public Theme getTheme() {
        return impl.getTheme() == null ? super.getTheme() : impl.getTheme();
    }

    @Override
    public ClassLoader getClassLoader() {
        return impl.getClassLoader();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        mRemoteActivity.onActivityResult(requestCode, resultCode, data);

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        mRemoteActivity.onStart();

        super.onStart();
    }

    @Override
    protected void onRestart() {
        mRemoteActivity.onRestart();

        super.onRestart();
    }

    @Override
    protected void onResume() {
        mRemoteActivity.onResume();
        setVisibleActivity(true); //设置界面再次可见

        super.onResume();
    }

    @Override
    protected void onPause() {
        mRemoteActivity.onPause();

        super.onPause();
    }

    @Override
    protected void onStop() {
        mRemoteActivity.onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mRemoteActivity.onDestroy();

        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mRemoteActivity.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mRemoteActivity.onRestoreInstanceState(savedInstanceState);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        mRemoteActivity.onNewIntent(intent);
        super.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
        mRemoteActivity.onBackPressed();
        if (mRemoteActivity.isBackFinish()) {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        return mRemoteActivity.onTouchEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);

        return mRemoteActivity.onKeyUp(keyCode, event);
    }


    @Override
    public void onWindowAttributesChanged(LayoutParams params) {
        if (mRemoteActivity != null) {
            mRemoteActivity.onWindowAttributesChanged(params);
        }

        super.onWindowAttributesChanged(params);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        mRemoteActivity.onWindowFocusChanged(hasFocus);

        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        mRemoteActivity.onCreateOptionsMenu(menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        mRemoteActivity.onOptionsItemSelected(item);

        return super.onOptionsItemSelected(item);
    }

    @Override
    public ComponentName startService(Intent service) {
        return super.startService(service);
    }

    /**
     * 获取插件包名
     *
     * @return
     */
    @Override
    public String getPluginPackageName() {
        String pluginPackageName = mRemoteActivity.getPackageName();
        if (TextUtils.isEmpty(pluginPackageName)) {
            pluginPackageName = getPackageName();
        }
        return pluginPackageName;
    }

    /**
     * 获取插件activity
     *
     * @return
     */
    @Override
    public Activity getPluginActivity() {
        return (Activity) mRemoteActivity;
    }

    /**
     * 标识插件activity不可见
     *
     * @return
     */
    @Override
    public boolean isVisibleActivity() {
        return visibleActivity;
    }

    @Override
    public void setVisibleActivity(boolean isVisibleActivity) {
        this.visibleActivity = isVisibleActivity;
    }
}
